<?

function generateLoginDot()
{
	$entityQuery = query::factory()
		->select()
		->from( 'entity' )
		->where( 'entity_login_id=:login_id' )
		->where( 'entity_primary = true' )
		->prepare();

	$login = query::factory()
	->select()
//	->select( '*,entity.entity_first_name as first_name, entity.entity_last_name as last_name' )
//	->select( 'affiliate_entity.entity_first_name as aff_first_name, affiliate_entity.entity_last_name as aff_last_name' )
	->from( 'login' )
	->join( 'entity', 'login.login_id = entity.entity_login_id and entity.entity_primary=true' )
/*
	
	->join( 'login   affiliatelogin', 'login.login_affiliate_login_id = affiliatelogin.login_id' )
	->join( 'entity affiliate_entity', 'affiliatelogin.login_id = affiliate_entity.entity_login_id and affiliate_entity.entity_primary=true' )
*/
	->order_by( 'login.login_affiliate_login_id, login.login_id' )
	->exec()
	->dbDataSource();

	echo 'digraph G {' . PHP_EOL;
	while( $login->fetch() )
	{
		//$loginNameRow = $entityQuery->bindValue( 'login_id', $login->login_id )->exec()->fetch();


		if ( $login->login_affiliate_login_id != '' )
		{
			$affiliateNameRow = $entityQuery->bindValue( 'login_id', $login->login_affiliate_login_id, ptINT )->exec()->fetch();
			if ( $affiliateNameRow )
			{
				echo "\t" .
				'"' . $login->entity_first_name . ' ' . $login->entity_last_name . '"' .
				' -> ' .
				'"' . $affiliateNameRow->entity_first_name . ' ' . $affiliateNameRow->entity_last_name . '"'
				. PHP_EOL;
			}
			else
			{
				echo "\t" .
				'"' . $login->entity_first_name . ' ' . $login->entity_last_name . '"' .
				' -> ' .
				'"ERROR-UNKNOWN"'
				. PHP_EOL;
			}
		}
	}

	echo '}';
}

